package com.bhkj.websocketlibrary.ws;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.ToastUtils;

/**
 * 采用构造者模式创建websoket请求
 */
public class WsClient {

    private WsTask wsTask;
    IWsCallback iWsCallback;

    public WsClient() {
        initClient();
    }

    private static class Holder {
        private static WsClient INSTANCE = new WsClient();
    }

    public static WsClient getInstance() {
        return Holder.INSTANCE;
    }

    private void initClient() {
        if (wsTask == null) {
            wsTask = new WsTask(mTaskHandler);
        }

        ThreadUtils.executeByFixed(1, wsTask);

        NetworkUtils.registerNetworkStatusChangedListener(new NetworkUtils.OnNetworkStatusChangedListener() {
            @Override
            public void onDisconnected() {
                notNetTip();
            }

            @Override
            public void onConnected(NetworkUtils.NetworkType networkType) {
                connect();
            }
        });
    }

    private void notNetTip() {
        Activity topActivity = ActivityUtils.getTopActivity();
        if (topActivity != null) {
            topActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showShort("网络不可用");
                }
            });
        }
    }

    public void connect() {
        if (wsTask != null) {
            wsTask.connect();
        }
    }

    public void sendMsg(String msg) {
        if (wsTask != null) {
            wsTask.sendMsg(msg);
        }
    }


    public void close() {
        if (wsTask != null) {
            wsTask.close();
        }
    }

    private void receiveMsg(Message msg) {
        String recMsg = (String) msg.obj;
        if (iWsCallback != null) {
            Activity topActivity = ActivityUtils.getTopActivity();
            if (topActivity != null) {
                topActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        iWsCallback.onMessage(recMsg);
                    }
                });
            }
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler mTaskHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            LogUtils.d(msg.obj);
            switch (msg.what) {
                case WsContact.OPEN:
                    break;
                case WsContact.RECEIVE_MSG:
                    receiveMsg(msg);
                    sendMsg("兄弟我已经收到");
                    break;
                case WsContact.EXCEPTION:
                    break;
                case WsContact.CLOSE_CLIENT:
                    break;
                case WsContact.CONNECT_SUCCESS:
                    break;
                case WsContact.CONNECT_ERROR:
                    break;
                default:
                    break;
            }
        }
    };

    public void addOnCallback(IWsCallback iWsCallback) {
        this.iWsCallback = iWsCallback;
    }

    public interface IWsCallback {
        void onMessage(String msg);
    }
}
